package com.goldsign.pay.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.goldsign.pay.common.log.SysLog;
import com.goldsign.pay.common.to.ServiceResponseDTO;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @ClassName: ApiHelpUtil
 * @Description: 
 * @author: huguang
 * @date: 2015-12-23 上午11:56:36
 *
 */
public class ApiHelpUtil {
	
	/**
	 * 字符串转换unicode
	 */
	public static String string2Unicode(String string) {
		if(StringUtils.isEmpty(string))return string;
	    StringBuffer unicode = new StringBuffer();
	    for (int i = 0; i < string.length(); i++) {
	        // 取出每一个字符
	        char c = string.charAt(i);
	        // 转换为unicode
	        unicode.append("\\u" + Integer.toHexString(c));
	    }
	    return unicode.toString();
	}

	
	/*
	 * 将返回转换json对象返回，并设置编码格式为utf-8
	 */
	public static void apiReturn(ServiceResponseDTO serviceResponse, HttpServletResponse response){
		String sign = SignUtil.signResponse(serviceResponse);
		serviceResponse.setSign(sign);

		String msg = serviceResponse.getResp_msg();
		
//		String unicode = string2Unicode(msg);
		
		serviceResponse.setResp_msg(msg);

		JSONObject json = (JSONObject)JSON.toJSON(serviceResponse);
//		String resp =  JSON.toJSONString(serviceResponse);
		try {
			SysLog.info(json.toString());
//			resp = Base64.encodeBase64String(ZipUtil.compress(resp).getBytes("ISO8859-1"));

		} catch (Exception e1) {
			e1.printStackTrace();
		}
		try {
			response.setContentType("text/html;charset=utf-8");   //解决返回中文乱码问题
			PrintWriter out = response.getWriter();  
			out.println(json.toString());
			out.flush();
            out.close();
		} catch (IOException e) {
			SysLog.error( e);
		}
	}

	public static String getRequestParams(HttpServletRequest request, String paramName, String defVal) {
		if (request == null || null == paramName || "".equals(paramName)) return null;
		String val = request.getParameter(paramName);
		if (val == null || "".equals(val)) {
			String tmp = (String) request.getAttribute(paramName);
			if (null != tmp && !"null".equals(tmp))
				val = tmp;
		}
		if (val != null) val = val.trim();
		if (StringUtils.isEmpty(val)) {
			val = defVal;
		}
		return val;
	}

	/**
	 * 通讯服务器异常
	 * @param response
	 * @param respMsg
	 */
	public static void writeServerError(HttpServletResponse response,
										String respMsg) {
		ServiceResponseDTO re=new ServiceResponseDTO(Constant.SYSTERM_ERROR,respMsg);
		apiReturn(re,response);
	}

	public static void writeToResponse(HttpServletResponse response,String content){
		try {

			response.setContentType("text/plain; charset=utf-8");
			PrintWriter writer = response.getWriter();
			writer.write(content);
			writer.flush();
		} catch (IOException e) {
			SysLog.error("");

		}
	}

	//签名失败
	public static void writeNoSig(HttpServletResponse response) {
		ServiceResponseDTO re=new ServiceResponseDTO(Constant.SIG_NOT_PASS,"签名不合法");
		apiReturn(re,response);
	}

}

